{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# -- coding: utf-8 --\n",
    "from pymongo import MongoClient\n",
    "client = MongoClient()\n",
    "import pandas\n",
    "from bson.code import Code\n",
    "import pickle\n",
    "import json\n",
    "\n",
    "db = client['RFID']\n",
    "\n",
    "collection = db['raw_data_192.168.0.69']\n",
    "#\n",
    "# tag_list = list(collection.distinct('data.EPC'))\n",
    "# mid_list = list(collection.distinct('measurement_uuid')).sort(\"measurement_uuid\",1)\n",
    "#\n",
    "# #print(a[0])\n",
    "# # rssi_table = db['rssi_table1']\n",
    "# # for tag in tag_list:\n",
    "# #     a = collection.find({\"data.EPC\":tag},{\"measurement_uuid\":1,\"data.RSSI\":1})\n",
    "# #     for node in a:\n",
    "# #         print(node)\n",
    "# #         rssi_table.insert({'tag_id': tag,'measurement_uuid':node['measurement_uuid'],'RSSI':node['data']})\n",
    "#\n",
    "# a = collection.find({\"data.EPC\":tag_list[0]},{\"measurement_uuid\":1,\"data.RSSI\":1}).sort( { \"measurement_uuid\": 1 } )\n",
    "# for node in a[:2]:\n",
    "#     print(node)\n",
    "#\n",
    "#\n",
    "# # tg1 = tag_list[0]\n",
    "# # tg2 = tag_list[1]\n",
    "# # a = collection.find({\"data.EPC\":tag},{\"measurement_uuid\":1,\"data.RSSI\":1})\n",
    "# # b = collection.find({\"data.EPC\":tag},{\"measurement_uuid\":1,\"data.RSSI\":1})\n",
    "# #\n",
    "# i,j = 0,0\n",
    "# while(i<len(mid_list),j<len(a)):\n",
    "\n",
    "mid_list = list(collection.distinct('measurement_uuid'))#.sort(\"measurement_uuid\",1)\n",
    "tag_list = list(collection.distinct('data.EPC'))\n",
    "collection = db['raw_data_192.168.0.70']\n",
    "tag_list += list(collection.distinct('data.EPC'))\n",
    "tag_set = set(tag_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "311 668\n",
      "18\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\xiaoli yang\\appdata\\local\\programs\\python\\python35\\lib\\site-packages\\ipykernel_launcher.py:3: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    },
    {
     "ename": "UnicodeDecodeError",
     "evalue": "'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-bb1ef19e3381>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'never_detected.pickle'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpik1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m     \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpickle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpik1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence"
     ]
    }
   ],
   "source": [
    "import xlrd\n",
    "xlsx = pandas.read_excel(open('EPC_Hashes.xlsx','rb'), sheet_name='Tabelle1')\n",
    "tag_hash = set(xlsx.ix[:,0])\n",
    "print(len(tag_hash), len(tag_set))\n",
    "never_detected = tag_hash - tag_set\n",
    "print(len(never_detected))\n",
    "#with open('never_detected.pickle','wb') as pik:\n",
    "#    pickle.dump(never_detected,pik)\n",
    "\n",
    "with open('never_detected.pickle', 'r') as pik1:\n",
    "    a = pickle.load(pik1)\n",
    "\n",
    "print(len(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "with open('neverDetected.csv', 'w') as outfile:\n",
    "     for s in never_detected:\n",
    "        outfile.write(s)\n",
    "        outfile.write(',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{'f2efef08d7d17641b4feded49653aa11', '35afada6382447f74ffa0fe52607bfca', '8496bb8754130c41197e2115c0e5537a', '2a69763c16cb0c04b3fb182dbc6f3b95', 'e387ba20dd772fae88cad478b8487edf', 'd2b193448f48e9ab7a3d70d30541ce3d', '332106459cc725a0a6e8523afc804df6', 'd02b7083d5f8118f925aee6f4670f5e9', 'a8b742ba6ab3870d570c97e5e22c9351', 'f96df3f9613043d05308a66305362b9c', '26e5bd47d63daf7eab3fa016c9dd2f9b', '969ee8d10d168c0f0b14628ad011c0f4', '6851a0cae99867bfb68e59f6c42dccd2', '54720a2ba397aab814f0981a844ffcbf', 'ad4a35f756a49408923d2c282defc120', '3cd474774e3fb676a702af25f76e67d3', 'ea9269cb7748ba45d512dddb8d0553fd', '886bd60f7c7017576d29b706f026589d'}\""
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
